package com.sat.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sat.goods.mapper.*;
import com.sat.goods.model.*;
import com.sat.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addGoods(Goods goods) {
        long spuid = IdWorker.getId();
        Spu spu = goods.getSpu();
        spu.setId(spuid);
        spuMapper.insert(spu);
        addSkus(goods, spu);
        return true;
    }

    @Override
    public SpuVo findBySpuid(Long spuid) {
        return goodsMapper.findBySpuid(spuid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateGoods(Goods goods) {
        Spu spu = goods.getSpu();
        spuMapper.updateById(spu);
        UpdateWrapper<Sku> skuUpdateWrapper = new UpdateWrapper<>();
        skuUpdateWrapper.eq("spu_id", spu.getId());
        skuMapper.delete(skuUpdateWrapper);
        addSkus(goods, spu);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        spuMapper.deleteById(id);
        UpdateWrapper<Sku> skuUpdateWrapper = new UpdateWrapper<>();
        skuUpdateWrapper.eq("spu_id", id);
        skuMapper.delete(skuUpdateWrapper);
        return true;
    }

    /**
     * 批量上架或下架
     * @param spuIds
     * @param status
     * @return
     */
    @Override
    public Boolean listUpAndDown(List<Long> spuIds, Integer status) {
        int i = goodsMapper.listUpAndDown(spuIds, status);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据skuId查询得到skuVo对象
     * @param skuId
     * @return
     */
    @Override
    public SkuVo getBySkuId(long skuId) {
        Sku sku = skuMapper.selectById(skuId);
        Spu spu = spuMapper.selectById(sku.getSpuId());
        SkuVo skuVo = new SkuVo();
        skuVo.setSku(sku);
        skuVo.setSpu(spu);
        return skuVo;
    }

    @Override
    public int updateInventory(String skuId, Integer num) {
        int i=skuMapper.updateInventory(skuId,num);
        return i;
    }

    private void addSkus(Goods goods, Spu spu) {
        String brandname = brandMapper.selectById(spu.getBrandId()).getName();
        Integer category3Id = spu.getCategory3Id();
        String categoryname = categoryMapper.selectById(category3Id).getName();
        Date date = new Date();
        for (Sku sku : goods.getSkuList()) {
            long skuid = IdWorker.getId();
            sku.setId(skuid);
            sku.setCreateTime(date);
            sku.setUpdateTime(date);
            sku.setSpuId(spu.getId());
            sku.setBrandName(brandname);
            sku.setCategoryId(category3Id);
            sku.setCategoryName(categoryname);
            ObjectMapper objectMapper = new ObjectMapper();
            String skuname = spu.getName();
            try {
                Map<String, String> map = objectMapper.readValue(sku.getSpec(), Map.class);
                for (String s : map.keySet()
                ) {
                    skuname += " " + map.get(s);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            sku.setName(skuname);
            skuMapper.insert(sku);
        }
    }

}
